<?php
namespace app\common\service;

use app\common\service\Base;
use think\Db;
class Cutting extends Base
{
    /**获取order裁剪人集合
	 * @author 飞燕
	 * @date 2020年9月27日
	 * @return array
	 */
    public function caichuangUser() {
        $systemUserKV = getSystemUserKV(null);
        $a=db('order')->group('caichuang_user_id')->column('caichuang_user_id');
        $caichuangUser = [];
        foreach ($a as $key => $value) {
            $b=arrayGet($systemUserKV, $value, '');
            if($b!=''){
                $caichuangUser[$value]=$b;
            }
        }
        return $caichuangUser;
    }
	/**
	 * 统计数据
	 *2020/11/19
	 * @return array
	 */
	public function getCuttingStat($factoryId=0,$where=[],$detailWhere=[]) {
		$cacheSuffix = ADMIN_ID;

		$whereAcOver=$whereAcNot=$whereAcNo=$where;
		//已裁床单数
		$caichuangOverCount=$caichuangOverAllQuantity=0;
		$count=db('order')->where($where)->field('count(id) as counts,sum(quantity) as all_quantity')->find();
		if(!empty($count)){
			$caichuangOverCount=$count['counts'];
			$caichuangOverAllQuantity=$count['all_quantity'];
		}
		//辅料发放单数
		$subQuery9 = db('distributing')->where("status in (1,2,3)")->distinct(true)->field('order_id')->buildSql();
		$whereAcOver[] = ['id', 'exp', Db::raw(" not in({$subQuery9})")];
		$acOverCount = db('order')->where($whereAcOver)->count('id');
		//辅料未发放单数
		$subQuery10 = db('distributing')->where("status in (1,2,3)")->distinct(true)->field('order_id')->buildSql();
		$whereAcNot[] = ['id', 'exp', Db::raw("in({$subQuery10})")];
		$acNotCount = db('order')->where($whereAcNot)->count('id');
		//未齐料单数
		$subQuery11 = db('order_material om')->join('material m', "om.material_id=m.id", 'left')->where("m.type = 'accessories' and om.caigou_show=1 and om.is_make_tag=0")->distinct(true)->field('om.order_id')->buildSql();
		$whereAcNo[] = ['id', 'exp', Db::raw("in({$subQuery11})")];
		$whereAcNo[] = ['status', 'in', [0,1]];
		$acNoCount=db('order')->where($whereAcNo)->count('id');
		// 整理结果
		$result = array();
		$result['caichuangOverCount'] = $caichuangOverCount;
		$result['caichuangAllQuantity'] = $caichuangOverAllQuantity;
		$result['acOverCount'] = $acOverCount;
		$result['acNotCount'] = $acNotCount;
		$result['acNoCount'] = $acNoCount;
		if($caichuangOverCount==0){
			$result['acOverCountPer'] = "0%";
			$result['acNotCountPer'] = "0%";
			$result['acNoCountPer'] = "0%";
		}else{
			$result['acOverCountPer'] = round($acOverCount/$caichuangOverCount*100)."%";
			$result['acNotCountPer'] = round($acNotCount/$caichuangOverCount*100)."%";
			$result['acNoCountPer'] = round($acNoCount/$caichuangOverCount*100)."%";
		}
		$result['detailWhere']=$detailWhere;
		return $result;
	}

}

